<?php

mb_internal_encoding("8bit");

if ($_SERVER['REQUEST_METHOD']=='POST'){
	if ($_FILES["fexcel"]["name"]==NULL){
		$error="<p id=\"thongbao\"> <img src=\"".ICON_DIR.'/error.gif'."\" alt=\"\"> vui lòng chọn file cần nhập";
		$div="<div class=\"error\">". $error."</div>";
		echo $div;
	}else if ($_FILES["fexcel"]["type"]!="application/vnd.ms-excel"){
			$error="<p id=\"thongbao\"> <img src=\"".ICON_DIR.'/error.gif'."\" alt=\"\"> Vui lòng chọn file excel";
			$div="<div class=\"error\">". $error."</div>";
			echo $div;
		}else{


			// xu ly nhap o day

			$file=Upload::uploadFile('fexcel',SRV_ROOT."/manager/upload/files/"); // lay ten file upload	
			$file_path=SRV_ROOT."/manager/upload/files/".$file; // duong dan toi file xls upload
		   // khoi tao
			$vt_row=6; // vi tri lay du lieu theo hang
			$data = new Spreadsheet_Excel_Reader($file_path,true,"UTF-8"); // khoi tao doi tuong doc file excel 
			$rowsnum = $data->rowcount($sheet_index=0); // lay so hang cua sheet
			$colsnum =  $data->colcount($sheet_index=0); // lay so cot cua sheet
			for ($i=$vt_row;$i<=$rowsnum;$i++){ 
					if ($data->val($i,2)!=NULL || $data->val($i,2)!=""){
						$gv=new GiangVien();
						$gv->setMa($data->val($i,2));
						$hoten=$data->val($i,3)." ".$data->val($i,4);
						$gv->setTen($hoten);
						$gv->setNgaySinh(NgayThang::dinhDangNhap($data->val($i,5)));
						$gv->setMatKhau("");
						$gv->setNamCongTac($data->val($i,6));
						$gv->setEmail($data->val($i,7));
						$gv->setMobile($data->val($i,8));
						$gv->setTel($data->val($i,9));
						$gv->setCmnd($data->val($i,10));
						$gt=new GioiTinh();
						$gt->getInfo($data->val($i,11));
						$gv->setGioiTinh($gt);
						
						//------------------------
						$strcv=$data->val($i,12);

						$adscv=explode(',',$strcv);

						$index=0;

						$dscv=array();

						foreach($adscv as $key){
							$cv=new ChucVu();
							$cv->getInfo($key);
							$dscv[$index]=$cv;
							$index++;
						}

						//------------
						
						
						$gv->setDSChucVu($dscv);
						$hv=new HocVi();
						$hv->getInfo($data->val($i,13));
						$gv->setHocVi($hv);
						$ttgv=new TinhTrangGV();
						$ttgv->getInfo($data->val($i,14));
						$gv->setTinhTrangGV($ttgv);
						$cn=new ChuyenNganh();
						$cn->getInfo($data->val($i,15));
						$gv->setChuyenNganh($cn);
						$gv->setHinhAnh("");
						$gv->setHeSoPhanCong(1.23);
						$gv->them();
					
				}	
			}

			//$thanh="<p id=\"thongbaotc\"> <img src=\"".ICON_DIR.'/notification-tick.gif'."\" alt=\"\"> Nập dữ liệu thành công";
			//$div="<div class=\"thanhcong\">". $thanh."</div>";
			//echo $div;
	}
}

		

?>

<form action="" method="POST" enctype="multipart/form-data">
	<fieldset>
		<legend>Nhập danh sách giảng viên từ Excel</legend>  
				<label class="inline">Chọn tập tin :</label>
				 <input type="file" name="fexcel"><br />
				 <input type="submit" value="Nhập dữ liệu" id="btthem2">
	</fieldset>
</form>